from random import randint
from math import cos, sin

class makeMatrix(object):
   """
   _Disp(self):              Displays a matrix of any size. Prints to the screen.
   _rand(self, l=1, h=10):   Makes every element of a matrix random from l to h
   _delete_row(self,row):    Removes (makes 0) an entire row in a matrix at row
   _delete_row(self,col):    Removes (makes 0) an entire column in a matrix at col
   _add(self, nmat):         Adds elements of nmat to the given matrix
   
   @add(namt1, nmat2): Returns a new matrix of the sum of the two given matrixes
   """
   def __init__(self, x=4, y=4):
      if x < 1 or y < 0: return
      self.Mat = []
      self.x = x
      self.y = y
      for X in range (1, x+1):
         for Y in range (1, y+1):
            if x==y and X == Y: self.Mat.append(1)
            else: self.Mat.append(0)
   
   def _Disp(self):
      for Y in range (0, self.y):
         #print self.Mat[X*self.y:X*self.y+self.x], X*self.y
         print self.Mat[Y*self.x:Y*self.x+self.x]
   
   def _rand(self, h=1, l=3):
      if l > h: h, l = l, h
      for X in range (0, self.x):
         for Y in range (0, self.y):
            self.Mat[Y+X*self.y] = randint(0,3)
      
   def _delete_row(self, row):
      if (row > self.y or col < 0) : return
      for X in range(0, self.x):
         self.Mat[X+row*self.x] = 0

   def _delete_col(self, col):
      if (col > self.x or col < 0) : return
      for Y in range(0, self.y):
         self.Mat[col+Y*self.x] = 0
         print col+Y*self.x
   
   def _add (self, nmat):
      if (self.x != nmat.x or self.y != nmat.y): return
      for X in range (0, self.x):
         for Y in range(0, self.y):
            self.Mat[Y+X*self.y] += nmat.Mat[Y+X*self.y]
   
   def _rotate44x (self, ang):
      if self.x != 4 or self.y != 4: return
      cosa = cos(ang)
      sina = sin(ang)
      TMat = makeMatrix(4,4)
      TMat.Mat = [1,   0,    0,   0, \
                  0, cosa, -sina, 0, \
                  0, sina,  cosa, 0, \
                  0,   0,    0,   1]
      self = self.mult(self, TMat)
      return self
   
   def _rotate44y (self, ang):
      if self.x != 4 or self.y != 4: return
      cosa = cos(ang)
      sina = sin(ang)
      TMat = makeMatrix(4,4)
      TMat.Mat = [cosa, 0, sina, 0, \
                    0,  1,   0,  0, \
                 -sina, 0, cosa, 0, \
                    0,  0,   0,  1]
      self = self.mult(self, TMat)
      return self
   
   def _rotate44z (self, ang):
      if self.x != 4 or self.y != 4: return
      cosa = cos(ang)
      sina = sin(ang)
      TMat = makeMatrix(4,4)
      TMat.Mat = [cosa, -sina, 0, 0, \
                  sina,  cosa, 0, 0, \
                    0,    0,   1, 0, \
                    0,    0,   0, 1]
      self = self.mult(self, TMat)
      return self

   @classmethod
   def add (cls, nmat1, nmat2):
      if (nmat1.x != nmat2.x or nmat1.y != nmat2.y): return
      tmp = makeMatrix(nmat1.x, nmat1.y)
      for X in range (0, nmat1.x):
         for Y in range(0, nmat1.y):
            tmp.Mat[Y+X*nmat1.y] = nmat1.Mat[Y+X*nmat1.y] + nmat2.Mat[Y+X*nmat1.y]
      return tmp

   @classmethod
   def mult (cls, nmat1, nmat2):
      if (nmat1.x != nmat2.y): return
      len, hei = nmat1.x, nmat1.y
      tmp = makeMatrix(len, hei)
      for Y in range (0, hei):
         for X in range(0, len):
            tmp.Mat[X+Y*len] = 0
            for N in range(0, len):
               tmp.Mat[X+Y*len] += nmat1.Mat[N+Y*len]*nmat2.Mat[X+N*len]
      return tmp

T1 = makeMatrix(4,4)

T1._Disp()
T1 = T1._rotate44x(45)
print
T1._Disp()


















